package neuralmusic.brain.gui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.List;

import javax.swing.JPanel;

public class FireDisplayPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	FireRecorder recorder;

	public FireDisplayPanel(FireRecorder recorder) {
		this.recorder = recorder;
		setBackground(Color.BLACK);
	}

	public void paint(Graphics g1) {
		super.paint(g1);
		Graphics2D g = (Graphics2D) g1;

		int nRow = recorder.size();

		int width = getWidth();
		int height = getHeight();

		int dy=10;
		
		g.setColor(Color.BLACK);

		g.clearRect(0, 0, width, height);

		if (nRow == 0)
			return;

		int rowH = (height-2*dy) / nRow;

		double marg = .001;
		double t1 = recorder.startTime() - marg;
		double t2 = recorder.endTime() + marg;

		double xScale;

		int xMarg=40;
		
		if (t1 == t2) {
			xScale = 1;
		} else {
			xScale = (width-xMarg) / (t2 - t1);
		}
		
		int yOff = dy;
		g.setColor(Color.RED);
		
		for (FireRecord rec : recorder.getRecords()) {

			int y1 = height - yOff;
			int y2 = height - rowH-yOff+4;
			g.drawString(rec.getLabel()+" "+rec.events().size(), 2, (y1+y2)/2);
			int x1 = xMarg;
			List<FireEvent> evs=rec.events();
			int n=evs.size();
			for (int i=0;i<n;i++) {
				FireEvent ev=rec.get(i);
				double t = ev.getTime();
				int x2 = xMarg+(int) (t * xScale);
				g.drawLine(x1, y1, x2, y1);
				g.drawLine(x2, y1, x2, y2);
				x1 = x2;
			}
			yOff+=rowH;
		}

	}

}
